import { Application } from "mydog";
import { UserInfo } from "../../../app/definition/userInfo";
import lobbyManager from "../../../app/lobbyManager";
import { cmd } from "../../../config/cmd";

declare global {
  interface Rpc { lobby: { main: Remote } }
}

export default class Remote {
  private app: Application;
  constructor(app: Application) {
    this.app = app;
  }

  /**
   * Add user into lobby
   * @param user
   */
  public addUser(user: { id: number, username: string, nickname: string, sid: string, token: string }) {
    const userInfo: UserInfo = new UserInfo(user.id, user.username, user.nickname, user.sid, user.token);
    lobbyManager.sendMessageToAll(cmd.lobby_onEnter, { user: { id: user.id, username: user.username, nickname: user.nickname } });
    lobbyManager.addUser(userInfo);
  }

  /**
   * Remove user out lobby
   * @param uid unique id for user
   * @param sid server id
   */
  public removeUser(uid: number, sid: string) {
    lobbyManager.removeUser(uid);
    lobbyManager.sendMessageToAll(cmd.lobby_onLeave, { id: uid });
  }

  public getUserByUsername(username: string) {
    return lobbyManager.getUserByUsername(username);
  }

  /**
   * Get users from lobby
   * @return {Array} users uids in lobby
   */
  private getUsers() {
    return lobbyManager.getUsers();
  }
}
